package com.java.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        // 判断是否是登录请求或跳转登录请求, 是则放行
        if (uri.indexOf("/login") != -1 || uri.indexOf("/indexSkip") != -1 || uri.indexOf("/login/permissions") != -1 ||
                uri.indexOf("/front/frontIndex") != -1 || uri.indexOf("/front/weChat") != -1 || uri.indexOf("/front/session") != -1 ||
                uri.indexOf("/front/frontLogin") != -1 || uri.indexOf("/front/frontReg") != -1 || uri.indexOf("/front/frontGetpass") != -1 ||
                uri.indexOf("/userLogin/message") != -1 || uri.indexOf("/userLogin/save") != -1 || uri.indexOf("/userLogin/advertisingLogin") != -1 ||
                uri.indexOf("/front/consulting") != -1 || uri.indexOf("/front/contribute") != -1 || uri.indexOf("/apply/files") != -1 ||
                uri.indexOf("/apply/file") != -1 || uri.indexOf("/front/topUp") != -1 || uri.indexOf("/front/addAmount") != -1 || uri.indexOf("/front/search") != -1 ||
                uri.indexOf("/apply/save") != -1 || uri.indexOf("/front/seoserve") != -1 || uri.indexOf("/order/orderList") != -1 ||
                uri.indexOf("/order/yes") != -1 || uri.indexOf("/order/no") != -1 || uri.indexOf("/mediaAccountLogin/login") != -1 ||
                uri.indexOf("/mediaAccountLogin/save") != -1 || uri.indexOf("/mediaAccount/message") != -1) {
            return true;
        }

        // 判断Session中是否存储了用户
        Object user = request.getSession().getAttribute("user");
        if (user != null) {
            return true;
        } else {
            // 如果没有登录则跳转到登录页面
            response.sendRedirect("/login");
        }
        return false;
    }
}
